package com.yuanxin.chooseimage;

import android.content.Context;
import android.graphics.Color;
import android.text.Layout;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceView;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.yuanxin.chooseimage.zxing.view.ViewfinderView;


public class ScanErCodeView extends ViewGroup {
    private ViewfinderView viewfinderView;
    private SurfaceView surfaceView;

    public ScanErCodeView(Context context) {//当View view = new View(context);
        this(context,null);
    }

    public ScanErCodeView(Context context, AttributeSet attrs) {//xml中定义，包含自定义属性
        this(context, attrs,-1);
    }

    public ScanErCodeView(Context context, AttributeSet attrs, int defStyle) {//也是xml定义，包含自定义属性和主题
        super(context, attrs, defStyle);
        viewfinderView = new ViewfinderView(context,attrs);
        surfaceView = new SurfaceView(context);
//        ViewGroup.MarginLayoutParams marginLayoutParams=new ViewGroup.MarginLayoutParams(100,100);
//        marginLayoutParams.topMargin=-(100);
//        surfaceView.setLayoutParams(marginLayoutParams);
//        setFitsSystemWindows(true);
        addView(surfaceView);
        addView(viewfinderView);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width = resolveSize(getResources().getDisplayMetrics().widthPixels,widthMeasureSpec);
        int height = resolveSize(getResources().getDisplayMetrics().heightPixels,heightMeasureSpec);
        Log.e("TAG","onMeasure-"+"width"+width+"height"+height);
        setMeasuredDimension(width,height);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        Log.e("TAG","onLayout---"+"left"+left+"top"+top+"right"+right+"bottom"+bottom);
        viewfinderView.layout(0, 0, right, bottom);
        surfaceView.layout(0, 0, right, bottom);
    }


    public ViewfinderView getViewfinderView() {
        return viewfinderView;
    }

    public SurfaceView getSurfaceView() {
        return surfaceView;
    }
}
